package com.ams.service.impl;

import com.ams.config.ComputingTime;
import com.ams.dao.EmployeeInformationMapper;
import com.ams.dao.SysDeptMapper;
import com.ams.pojo.Attendance;
import com.ams.dao.AttendanceMapper;
import com.ams.pojo.EmployeeInformation;
import com.ams.pojo.SysDept;
import com.ams.service.AttendanceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 考勤表 服务实现类
 * </p>
 *
 * @author LK
 * @since 2021-11-11
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired(required = false)
    AttendanceMapper attendanceMapper;
    @Autowired(required = false)
    SysDeptMapper sysDeptMapper;
    @Autowired(required = false)
    private EmployeeInformationMapper employeeInformationMapper;

    @Override
    public Map<String, Object> selectList(Attendance a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //创建分页对象
        Page<Attendance> page = new Page<>(a.getPage(), a.getRow());
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();

        //条件查询
        queryWrapper.eq(a.getStaffId() != null, "staff_id", a.getStaffId());
        queryWrapper.ge(a.getBeginTime() != null && a.getBeginTime() != "", "clock_date", a.getBeginTime());
        queryWrapper.le(a.getEndTime() != null && a.getEndTime() != "", "clock_date", a.getEndTime());
        queryWrapper.like(a.getName() != null && a.getName() != "", "staff_name", a.getName());
        queryWrapper.eq(a.getDepartmentSelect() != null && !"全部部门".equals(a.getDepartmentSelect()) && a.getDepartmentSelect() != "", "department", a.getDepartmentSelect());
        queryWrapper.orderByDesc("clock_date");
        //获取分页结果集
        Page<Attendance> p = attendanceMapper.selectPage(page, queryWrapper);

        //获取部门列表
        List<SysDept> sysDepts = sysDeptMapper.selectList(null);
        SysDept sysDept = new SysDept();
        sysDept.setDeptName("全部部门");
        try {
            sysDept.setDownTime(null);
            sysDept.setUpTime(null);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sysDept.setDeptName("全部部门");
        sysDepts.add(sysDept);

        map.put("list", p.getRecords());
        map.put("depts", sysDepts);
        //分页总条数
        map.put("total", p.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> signin(Attendance a) throws ParseException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Date date = new Date();
        //获取当前日期的年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前时间的时分秒
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
        String nowDate = sdf.format(date);//当前时间的年月日
        String nowTime = sdf1.format(date);//当前时间的时分秒

        //设置打卡日期为当天打卡日期
        a.setClockDate(nowDate);
        //通过打卡日期和用户id查询数据
        List listStaffId = attendanceMapper.selectByStaffIdList(a);
        //判断是否有多条相同数据
        if (listStaffId.size() == 1) {
            //查询登录人当天的信息
            Attendance attendance = attendanceMapper.selectByStaffId(a);
            //判断是否存在当天的数据
            if (attendance.getDepartment() != null && attendance.getStaffName()!=null) {
                //判断是否缺勤
                if (!"缺勤".equals(attendance.getRemarks())) {
                    //查询上班时间
                    SysDept sysDept = sysDeptMapper.selectByTime(attendance.getDepartment());
                    //计算迟到时间
                    ComputingTime computingTime = new ComputingTime();
                    String timeDiffer = computingTime.ctime(nowTime, sysDept.getUpTime());

                    //比较时间大小  nowTime<sysDept.getDownTime()时值小于零
                    int res = nowTime.compareTo(sysDept.getUpTime());
                    //判断是否重复签到
                    if (attendance.getClockInTime() == null) {
                        attendance.setClockInTime(nowTime);
                        //判断是否迟到
                        if (res < 0) {
                            attendance.setRemarks("未迟到");
                        } else {
                            attendance.setRemarks("迟到");
                            EmployeeInformation emp = new EmployeeInformation();
                            emp.setStaffId(a.getStaffId());
                            EmployeeInformation employeeInformation = employeeInformationMapper.selectByStaffId(emp);
                            employeeInformation.setLateTime(employeeInformation.getLateTime() - 1);
                            employeeInformationMapper.updateById(employeeInformation);

                        }
                        int num = attendanceMapper.updateById(attendance);
                        //判断是否打卡成功
                        if (num > 0) {
                            if (attendance.getRemarks().equals("迟到")) {
                                map.put("info", "成功迟到了:" + timeDiffer);
                            } else {
                                map.put("info", "签到成功");
                            }
                        } else {
                            map.put("info", "签到失败，请联系管理员");
                        }
                    } else {
                        map.put("info", "已经成功打卡上班，不能重复打");
                    }
                } else {
                    map.put("info", "你已经超过下班时间打卡，已经是缺勤");
                }
            } else {
                map.put("info", "今天的数据有误，签到失败，请联系管理员");
            }
        } else {
            map.put("info", "请联系管理员，打卡机出现问题");
        }
        return map;
    }

    @Override
    public Map<String, Object> signout(Attendance a) throws ParseException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Date date = new Date();
        //获取当前日期的年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前时间的时分秒
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
        String nowDate = sdf.format(date);//当前时间的年月日
        String nowTime = sdf1.format(date);//当前时间的时分秒

        //设置打卡日期为当天打卡日期
        a.setClockDate(nowDate);
        //通过打卡日期和用户id查询数据
        List listStaffId = attendanceMapper.selectByStaffIdList(a);
        if (listStaffId.size() == 1) {
            Attendance attendance = attendanceMapper.selectByStaffId(a);
            if (!"缺勤".equals(attendance.getRemarks())) {
                //通过部门查询下班时间
                SysDept sysDept = sysDeptMapper.selectByTime(attendance.getDepartment());
                //计算加班时间
                ComputingTime computingTime = new ComputingTime();
                String timeDiffer = computingTime.ctime(nowTime, sysDept.getDownTime());
                //判断是否重复打卡
                if (attendance.getClockOutTime() == null) {
                    if (attendance != null && attendance.getClockInTime() != null) {
                        //设置下班打开时间
                        attendance.setClockOutTime(nowTime);

                        //比较时间大小  nowTime<sysDept.getDownTime()时值小于零
                        int res = nowTime.compareTo(sysDept.getDownTime());
                        //判断是否早退
                        if (res < 0) {
                            if (attendance.getRemarks().equals("未迟到")) {
                                attendance.setRemarks("早退");
                            } else {
                                attendance.setRemarks("迟到,早退");
                            }
                            EmployeeInformation emp = new EmployeeInformation();
                            emp.setStaffId(a.getStaffId());
                            EmployeeInformation employeeInformation = employeeInformationMapper.selectByStaffId(emp);
                            employeeInformation.setEarlyTime(employeeInformation.getEarlyTime() - 1);
                            employeeInformationMapper.updateById(employeeInformation);
                        } else {
                            if (attendance.getRemarks().equals("未迟到")) {
                                attendance.setRemarks("正常出勤");
                            } else {
                                attendance.setRemarks("迟到");
                            }
                        }
                        int num = attendanceMapper.updateById(attendance);
                        //判断是否打开成功
                        if (num > 0) {
                            if (attendance.getRemarks().equals("早退")) {
                                map.put("info", "成功早退了");
                            } else if (attendance.getRemarks().equals("迟到,早退")) {
                                map.put("info", "又迟到，又早退了");
                            } else {
                                map.put("info", "打卡成功，加班了：" + timeDiffer);
                            }
                        } else {
                            map.put("info", "打卡失败，请联系管理员");
                        }
                    } else {
                        map.put("info", "今天上班未打开，打卡失败");
                    }
                } else {
                    map.put("info", "你已经打卡下班了，不能重复打");
                }
            } else {
                map.put("info", "你上班没有及时打卡，超过了下班时间，已经是缺勤");
            }
        } else {
            map.put("info", "请联系管理员，打卡机出现问题");
        }
        return map;
    }
}
